import request from '@api/request';
import { HttpUrl } from '@api/httpUrl';

// 注册
export function register(form: AjaxRequest['registerForm']) {
    return request.post<AjaxResponse['loginData']>(HttpUrl.register, { ...form });
}

// 登录
export function login(form: AjaxRequest['loginForm']) {
    return request.post<AjaxResponse['loginData']>(HttpUrl.login, { ...form });
}

// 更该密码
export function changePassword(newPassword: string, email: string, authCode: string) {
    return request.post<AjaxResponse['loginData']>(HttpUrl.changePassword, { newPassword, email, authCode });
}

// 无感登录，刷新token
let promise: Promise<any> | null = null;
export async function refreshToken() {
    if (promise) {
        return await promise;
    }
    promise = new Promise((resolve) => {
        resolve(request.get(HttpUrl.refreshToken));
    });
    promise.finally(() => {
        promise = null;
    });
    return await promise;
}

// 获取用户权限路由
export function getRoutes() {
    return request.get(HttpUrl.getRoutes, { headers: { 'Cache-Control': 'no-cache' } });
}
